package com.jxs.android.sxyw;

import java.io.IOException;
import java.util.Date;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.xmlpull.v1.XmlPullParserException;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jxs.android.soap.BuWebService;
import com.jxs.android.soap.IWebService;
import com.jxs.android.sxyw.boot.SelfApplication;
import com.jxs.android.sxyw.data.EomsInfo;
import com.jxs.android.sxyw.data.HitchInfo;
import com.jxs.android.sxyw.weiget.PopMsg;
import com.jxs.android.sxyw.weiget.TopBar;
import com.jxs.android.sxyw.weiget.WheelDateTimeDialog;
import com.jxs.android.util.StartHelper;
import com.jxs.android.util.StringUtils;

public class EomsAlermProcess extends AbstractActivity implements OnClickListener {

	private final int REQ_HITCH_REASON = 0x003;
	private final int REQ_HITCH_REASON_SUB = 0x004;
	private final int REQ_HITCH_REASON_RESULT = 0x005;
	private final int REQ_HITCH_TIME = 0x006;
	private final int REQ_SPECIFIC_STEP = 0x007;

	private TextView //
	        eoms_info_hitch_reason,// 故障原因
	        eoms_info_hitch_reason_sub,// 故障原因细分
	        eoms_info_hitch_reason_result,// 故障原因结果
	        eoms_info_accepte_process_time,// 采取处理措施时间
	        eoms_info_specific_step;// 具体处理措施

	private CheckBox eoms_info_net_work_changed,// 实施网络变更
	        eoms_info_end_plan;// 为最终计划

	private LinearLayout //
	        eoms_hitch_reason_ll,//
	        eoms_hitch_reason_sub_ll,//
	        eoms_hitch_reason_result_ll,//
	        eoms_net_work_changed_ll,//
	        eoms_end_plan_ll,//
	        eoms_accepte_process_time_ll,//
	        eoms_specific_step_ll;
	private EomsInfo eomsInfo;
	private ProgressDialog progressDialog = null;

	private String bugType;
	private String bugInd;

	protected void onCreate(android.os.Bundle savedInstanceState) {
		initTopBar(savedInstanceState, R.layout.eoms_alarm_process);

		if (null == SelfApplication.getUserInfo() || StringUtils.isEmpety(SelfApplication.getUserInfo().getTicket())) {
			PopMsg.show("登录已过期,请重新登录");
			StartHelper.startActivity(Login.class);
			return;
		}

		eoms_hitch_reason_ll = (LinearLayout) findViewById(R.id.eoms_hitch_reason_ll);
		eoms_hitch_reason_sub_ll = (LinearLayout) findViewById(R.id.eoms_hitch_reason_sub_ll);
		eoms_hitch_reason_result_ll = (LinearLayout) findViewById(R.id.eoms_hitch_reason_result_ll);
		eoms_net_work_changed_ll = (LinearLayout) findViewById(R.id.eoms_net_work_changed_ll);
		eoms_end_plan_ll = (LinearLayout) findViewById(R.id.eoms_end_plan_ll);
		eoms_accepte_process_time_ll = (LinearLayout) findViewById(R.id.eoms_accepte_process_time_ll);
		eoms_specific_step_ll = (LinearLayout) findViewById(R.id.eoms_specific_step_ll);

		eoms_info_hitch_reason = (TextView) findViewById(R.id.eoms_info_hitch_reason);
		eoms_info_hitch_reason_sub = (TextView) findViewById(R.id.eoms_info_hitch_reason_sub);
		eoms_info_hitch_reason_result = (TextView) findViewById(R.id.eoms_info_hitch_reason_result);
		eoms_info_net_work_changed = (CheckBox) findViewById(R.id.eoms_info_net_work_changed);
		eoms_info_end_plan = (CheckBox) findViewById(R.id.eoms_info_end_plan);
		eoms_info_accepte_process_time = (TextView) findViewById(R.id.eoms_info_accepte_process_time);
		eoms_info_specific_step = (TextView) findViewById(R.id.eoms_info_specific_step);

		eoms_info_hitch_reason_result.setText("已解决");
		eoms_hitch_reason_ll.setOnClickListener(this);
		eoms_hitch_reason_sub_ll.setOnClickListener(this);
		eoms_hitch_reason_result_ll.setOnClickListener(this);

		netWorkChanged();
		endPanChanged();

		eoms_accepte_process_time_ll.setOnClickListener(this);
		eoms_specific_step_ll.setOnClickListener(this);

		if (null != getIntent() && null != getIntent().getExtras()) {
			eomsInfo = (EomsInfo) getIntent().getExtras().get("eomsInfo");
		}
	}

	private int SUBMIT = 0x00922;

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (null != progressDialog && progressDialog.isShowing()) {
				progressDialog.dismiss();
			}
			if (msg.what == SUBMIT) {
				String status = msg.obj.toString();
				if ("0".equals(status)) {
					setResult(RESULT_OK);
					finish();
				} else if ("-1".equals(status)) {
					PopMsg.show("登录已过期,请重新登录");
					StartHelper.startActivity(Login.class);
					finish();
				} else if ("-2".equals(status)) {
					PopMsg.show("服务端异常");
				} else if ("-3".equals(status)) {
					PopMsg.show("输入参数无效");
				}
			}
		}

	};

	/**
	 * @ticket
	 * @case_no
	 * @bugType
	 * @bugInd
	 * @faultDealResult
	 * @ifExcuteNetChange
	 * @ifFinallySolveProject
	 * @processTime
	 * @processText
	 */
	private void getNoticeDetail(final String hitch_reason_result, final String accepte_process_time, final String specific_step) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				String wsdl_url = SelfApplication.WSDL_URL;// wsdl
				String namespace = SelfApplication.WSDL_NAME_SPACE;// namespace
				String methodName = "processWorkOrderToReply1";// 要调用的方法名称
				SoapObject request = new SoapObject(namespace, methodName);
				request.addProperty("ticket", SelfApplication.getUserInfo().getTicket());
				request.addProperty("case_no", eomsInfo.getCase_no());
				request.addProperty("bugType", bugType);
				request.addProperty("bugInd", bugInd);
				request.addProperty("faultDealResult", hitch_reason_result);
				request.addProperty("ifExcuteNetChange", eoms_info_net_work_changed.isChecked() ? "Y" : "N");
				request.addProperty("ifFinallySolveProject", eoms_info_end_plan.isChecked() ? "Y" : "N");
				request.addProperty("processTime", accepte_process_time);
				request.addProperty("processText", specific_step);
				request.addProperty("workOrderType", eomsInfo.getOrderType().index + "");
				BuWebService webService = new BuWebService(wsdl_url, request);
				webService.setDoNet(false);
				webService.requestWSDL(new IWebService() {
					@Override
					public void logic(SoapSerializationEnvelope envelope) throws IOException, XmlPullParserException {
						paserSubmit(envelope.getResponse().toString());
					}
				});
			}
		}).start();
	}

	private void paserSubmit(String stauts) {
		Message msg = new Message();
		msg.what = SUBMIT;
		msg.obj = stauts;
		handler.sendMessage(msg);
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == eoms_hitch_reason_ll.getId()) {
			Intent intent = new Intent(this, SelectHitch.class);
			Bundle bundle = new Bundle();
			bundle.putInt("flag", 1);
			bundle.putString("title", "故障原因");
			intent.putExtras(bundle);
			startActivityForResult(intent, REQ_HITCH_REASON);
		} else if (v.getId() == eoms_hitch_reason_sub_ll.getId()) {
			if (StringUtils.isEmpety(bugType)) {
				PopMsg.show("请先选择故障原因！");
				return;
			}
			Intent intent = new Intent(this, SelectHitch.class);
			Bundle bundle = new Bundle();
			bundle.putInt("flag", 2);
			bundle.putString("title", "故障原因细分");
			bundle.putString("parent_id", bugType);
			intent.putExtras(bundle);
			startActivityForResult(intent, REQ_HITCH_REASON_SUB);
		} else if (v.getId() == eoms_hitch_reason_result_ll.getId()) {
			Intent intent = new Intent(this, SelectHitchResult.class);
			startActivityForResult(intent, REQ_HITCH_REASON_RESULT);
		} else if (v.getId() == eoms_accepte_process_time_ll.getId()) {
			java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			showDateTime(REQ_HITCH_TIME, df.format(new Date()));
		} else if (v.getId() == eoms_specific_step_ll.getId()) {
			requestEditText("处理措施", eoms_info_specific_step.getText().toString(), REQ_SPECIFIC_STEP, 300);
		}
	}

	private void netWorkChanged() {
		eoms_net_work_changed_ll.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View paramView) {
				if (eoms_info_net_work_changed.isChecked()) {
					eoms_info_net_work_changed.setChecked(false);
				} else {
					eoms_info_net_work_changed.setChecked(true);
				}
			}
		});
	}

	private void endPanChanged() {

		eoms_end_plan_ll.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View paramView) {
				if (eoms_info_end_plan.isChecked()) {
					eoms_info_end_plan.setChecked(false);
				} else {
					eoms_info_end_plan.setChecked(true);
				}
			}
		});
	}

	private void showDateTime(int requestCode, String currentTime) {
		WheelDateTimeDialog dateTimeDialog = new WheelDateTimeDialog(this, requestCode, currentTime);
		dateTimeDialog.setDateTimeChangeListener(new WheelDateTimeDialog.DateTimeChangeListener() {
			@Override
			public void onKeySave(int requestCode, String dateTimeValue) {
				if (REQ_HITCH_TIME == requestCode) {
					eoms_info_accepte_process_time.setText(dateTimeValue);
				}
			}
		});
		dateTimeDialog.show();
	}

	@Override
	public void topBarLogic(TopBar topBar) {
		topBar.setTitle(R.string.work_order_reply);
		initTopBarReturn(topBar);
		topBar.setLogicBtnText("提交");
		topBar.setTopBarBtnListern(new TopBar.LogicBtnListern() {
			@Override
			public void onClickLogicBtn() {

				if (StringUtils.isEmpety(bugType)) {
					PopMsg.show("请选择故障原因！");
					return;
				}
				if (StringUtils.isEmpety(bugInd)) {
					PopMsg.show("请选择故障原因细分！");
					return;
				}

				String hitch_reason_result = eoms_info_hitch_reason_result.getText().toString();
				String accepte_process_time = eoms_info_accepte_process_time.getText().toString();
				String specific_step = eoms_info_specific_step.getText().toString();

				if (StringUtils.isEmpety(hitch_reason_result)) {
					PopMsg.show("请填写故障原因结果！");
					return;
				}
				if (StringUtils.isEmpety(accepte_process_time)) {
					PopMsg.show("请选择采取措施时间！");
					return;
				}
				if (StringUtils.isEmpety(specific_step)) {
					PopMsg.show("请填写处理措施！");
					return;
				}
				getNoticeDetail(hitch_reason_result, accepte_process_time, specific_step);
			}
		});

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == REQ_SPECIFIC_STEP && resultCode == RESULT_OK) {
			eoms_info_specific_step.setText(data.getExtras().getString("desc"));
		} else if (requestCode == REQ_HITCH_REASON_RESULT && resultCode == RESULT_OK) {
			eoms_info_hitch_reason_result.setText(data.getExtras().getString("hitchResult"));
		} else if (requestCode == REQ_HITCH_REASON && resultCode == RESULT_OK) {
			Bundle bundle = data.getExtras();
			if (null != bundle) {
				HitchInfo hitchInfo = (HitchInfo) bundle.getSerializable("hitchInfo");
				bugType = hitchInfo.getId();
				eoms_info_hitch_reason.setText(hitchInfo.getName());
			}
		} else if (requestCode == REQ_HITCH_REASON_SUB && resultCode == RESULT_OK) {
			Bundle bundle = data.getExtras();
			if (null != bundle) {
				HitchInfo hitchInfo = (HitchInfo) bundle.getSerializable("hitchInfo");
				bugInd = hitchInfo.getId();
				eoms_info_hitch_reason_sub.setText(hitchInfo.getName());
			}
		}
	}
}
